# from sqlalchemy import create_engine, Column, Integer, String
# from sqlalchemy.orm import sessionmaker
# from sqlalchemy.ext.declarative import declarative_base
#
# # 创建数据库连接
# engine = create_engine('your_database_connection_string')
#
# # 创建会话工厂
# Session = sessionmaker(bind=engine)
#
# # 创建基类
# Base = declarative_base()
#
# # 自动生成模型类
# def generate_model_class(table_name, fields):
#     # 定义模型类
#     class Model(Base):
#         __tablename__ = table_name  # 表名
#         id = Column(Integer, primary_key=True)
#         # 动态创建字段
#         for field in fields:
#             locals()[field] = Column(String)
#
#     return Model
#
# # 自动生成增删改查方法
# def generate_crud_methods(Model):
#     # 增加数据
#     def add_data(data):
#         session = Session()
#         obj = Model(**data)
#         session.add(obj)
#         session.commit()
#         session.close()
#
#     # 删除数据
#     def delete_data(data_id):
#         session = Session()
#         obj = session.query(Model).get(data_id)
#         if obj:
#             session.delete(obj)
#             session.commit()
#         session.close()
#
#     # 更新数据
#     def update_data(data_id, data):
#         session = Session()
#         obj = session.query(Model).get(data_id)
#         if obj:
#             for key, value in data.items():
#                 setattr(obj, key, value)
#             session.commit()
#         session.close()
#
#     # 查询数据
#     def get_data(data_id):
#         session = Session()
#         obj = session.query(Model).get(data_id)
#         session.close()
#         return obj
#
#     return add_data, delete_data, update_data, get_data
#
# # 示例用法
# table_name = 'your_table_name'
# fields = ['name', 'age']  # 根据表中字段进行定义
# Model = generate_model_class(table_name, fields)
# add_data, delete_data, update_data, get_data = generate_crud_methods(Model)
#
# # 使用自动生成的方法操作数据
# data = {'name': 'Alice', 'age': '25'}
# add_data(data)
#
# delete_data(1)
#
# data_id = 2
# new_data = {'name': 'Bob', 'age': '30'}
# update_data(data_id, new_data)
#
# user = get_data(2)
# if user:
#     print(f"User: {user.name}, Age: {user.age}")
# else:
#     print("User not found.")
